import hypermedia.video.*;

OpenCV opencv;                    
PImage img;
PImage edgeImg;

void setup() { 
  size(640, 480);
    opencv = new OpenCV( this );    
  //opencv.capture(width/2, height/2);
  opencv.movie( "clip1.mov", width/2, height/2 );
  
}

void draw() {
  opencv.read();
  //image(img, 0, 0); // Displays the image from point (0,0)
  //opencv.blur(opencv.GAUSSIAN, 5); //prev 21
  //opencv.convert(opencv.GRAY);
  //opencv.threshold(50);
  PImage img = opencv.image();
  image(opencv.image(),0,0,640,480);
  img.loadPixels();
  
  // Create an opaque image of the same size as the original
  edgeImg = createImage(img.width, img.height, ALPHA);
  
  //Uncomment this to see JUST the black edge detected lines on a yellow background
   //background(255, 204, 0); 
   
  // Loop through every pixel in the image.
  for (int y = 1; y < img.height; y++) { // Skip top and bottom edges
    for (int x = 1; x < img.width; x++) { // Skip left and right edges
    
            // Pixel location and color
      int loc = x + y*img.width;
      color pix = img.pixels[loc];
      
      // Pixel to the left location and color
      int leftLoc = (x - 1) + y*img.width;
      color leftPix = img.pixels[leftLoc];
      
      //Top px location and color
      int topLoc = x + (y-1)*img.width;
      color topPix = img.pixels[topLoc];
      
      // New color is difference between pixel and left neighbor
      float diff2 = abs(brightness(pix) - brightness(topPix));
      float diff = abs(brightness(pix) - brightness(leftPix));
      diff = Math.max(diff, diff2);
      
      //this draws the edge detected lines in RED to a new image
        edgeImg.pixels[loc] = color(255, 0, 0, diff*2); 
    }
  }
  // State that there are changes to edgeImg.pixels[]
  edgeImg.updatePixels();
  image(edgeImg, 0, 0, 640, 480); // Draw the new image
  g.removeCache(edgeImg);
}

